<?php

/*
Plugin Name: Membership Dues
Plugin URI: 
Description: Allows marking of membership dues
Version: 1.00
Author: Brady Emerson
Author URI: http://www.bradyemerson.com
*/

/****************************************************************
 * CHANGELOG
 *
 * 1.00 - Initial Release
 *
 ****************************************************************/

/****************************************************************
 * Begin Code for Extra Profile Fields
 ***************************************************************/

/* this one users will see on their profiles */
//add_action( 'show_user_profile', 'user_profile' );
function user_profile( $user ) {
	
}

/* this one admins will see when they go view another user */
add_action( 'edit_user_profile', 'admin_profile' );
function admin_profile( $user ) { 
	$metaValue = get_user_meta( $user->ID, 'membership_dues_paid', true );
	$duesPaid = ($metaValue && $metaValue == 'true');
    ?>
	<h3><?php _e("Membership Dues"); ?></h3>
	<table class="form-table">
		<tr>
			<th scope="row"><?php _e("Membership Dues"); ?></th>
			<td>
				<label for="membership_paid">
					<input type="checkbox" name="membership_paid" id="membership_paid" <?php if($duesPaid) echo "checked='checked'"; ?> />
					Dues Are Paid
				</label>
			</td>
		</tr>
	</table>
<?php }
 
add_action( 'profile_update', 'save_membership_fields' );
function save_membership_fields( $user_id ) {
	if ( !current_user_can( 'Edit Membership Dues' ) ) { return false; }
	if ($_POST['membership_paid'] == 'on') {
		update_user_meta( $user_id, 'membership_dues_paid', 'true' );
	} else {
		delete_user_meta( $user_id, 'membership_dues_paid');
	}
}

// Activation
register_activation_hook(__FILE__, 'memeberdues_activation');
function memeberdues_activation() {
	// create the basic role
	add_role('vp_finance', 'VP of Finance', array(
		'read' => true, // True allows that capability
	));

	// Set the capabilities for the vp of finance
	$role = get_role('vp_finance');
	// We need this role, no other chance
	if ( empty($role) ) {
		return;
	}
	
	$role->add_cap('Edit Membership Dues');
	$role->add_cap('list_users');
	$role->add_cap('edit_users');
}

// Deactivation
register_deactivation_hook(__FILE__, 'memeberdues_deactivation');
function memeberdues_deactivation() {
	global $wpdb;
	
	$role = get_role('vp_finance');
	// We need this role, no other chance
	if ( empty($role) ) {
		return;
	}
	
	$query = "SELECT ID FROM {$wpdb->usermeta} INNER JOIN {$wpdb->users} "
		. "ON {$wpdb->usermeta}.user_id = {$wpdb->users}.ID "
		. "WHERE meta_key='{$wpdb->prefix}capabilities' AND meta_value LIKE '%vp_finance%';";

	$users = $wpdb->get_results($query);
	$count = count($users);

	foreach ( $users as $u ) {
		$user = new WP_User($u->ID);
		if ( $user->has_cap('vp_finance') ) {		// Check again the user has the deleting role
			$user->set_role('subscriber');
		}
	}
	
	$role->remove_cap('Edit Membership Dues');
	$role->remove_cap('list_users');
	$role->remove_cap('edit_users');
	remove_role('vp_finance');
}